package com.gxy.learn.securityauth.auth.filter;

import com.alibaba.fastjson.JSONObject;
import com.gxy.learn.securityauth.common.Result;
import com.gxy.learn.securityauth.config.JwtProperties;
import com.gxy.learn.securityauth.jwt.JwtUtil;
import com.gxy.learn.securityauth.jwt.JwtVersionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description(登出成功)
 * @author: Gao xueyong
 * Create at: 2022/02/17 下午15:43
 */
@Slf4j
@Component
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    @Autowired
    private JwtVersionUtil jwtVersionUtil;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        String authToken = request.getHeader(JwtProperties.getHeader());
        //刷新版本号
        jwtVersionUtil.refreshLoginVersion(JwtUtil.getUsername(authToken));
        log.info("登出成功！");
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(JSONObject.toJSONString(Result.success("登出成功!")));
    }
}
